#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Git工作流测试模块
"""


class GitWorkflow:
    """Git工作流类，用于模拟Git操作"""
    
    def __init__(self):
        """初始化Git工作流"""
        self.commits = []
        self.current_branch = "main"
        self.bugs = []  # 新增bug列表
    
    def create_branch(self, branch_name):
        """创建新分支"""
        print(f"创建分支: {branch_name}")
        self.current_branch = branch_name
        
    def commit(self, message):
        """提交更改"""
        print(f"在分支 {self.current_branch} 上提交: {message}")
        self.commits.append({
            "branch": self.current_branch,
            "message": message
        })
        
    def merge_branch(self, source_branch, target_branch):
        """合并分支"""
        print(f"将分支 {source_branch} 合并到 {target_branch}")
        self.current_branch = target_branch
        
    def show_history(self):
        """显示提交历史"""
        print("提交历史:")
        for i, commit in enumerate(self.commits, 1):
            print(f"{i}. [{commit['branch']}] {commit['message']}")
    
    def report_bug(self, bug_description):
        """报告bug"""
        print(f"报告bug: {bug_description}")
        self.bugs.append(bug_description)
    
    def fix_bug(self, bug_description, fix_description):
        """修复bug"""
        if bug_description in self.bugs:
            print(f"在分支 {self.current_branch} 上修复bug: {bug_description}")
            print(f"修复方案: {fix_description}")
            self.bugs.remove(bug_description)
            self.commit(f"修复bug: {bug_description}")
        else:
            print(f"未找到bug: {bug_description}")


def simulate_workflow():
    """模拟Git工作流"""
    workflow = GitWorkflow()
    workflow.commit("初始提交")
    workflow.create_branch("feature-branch")
    workflow.commit("添加新功能")
    workflow.merge_branch("feature-branch", "main")
    workflow.commit("完善功能")
    
    # 新增：报告bug和创建hotfix分支修复
    print("\n===== 发现并修复bug =====")
    workflow.report_bug("登录功能异常")
    workflow.create_branch("hotfix/login-bug")
    workflow.fix_bug("登录功能异常", "修复了用户认证逻辑")
    workflow.merge_branch("hotfix/login-bug", "main")
    workflow.commit("合并hotfix分支")
    
    workflow.show_history()
    
    return workflow


if __name__ == "__main__":
    simulate_workflow()